package cn.tedu._03_vehicle.base.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

@Component
//封装类，封装好之后，方便调用
public class CacheUtil {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    //封装字符串类型的set方法
    public void set(String key, Serializable value){
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        ops.set(key, value);
    }
    //封装字符串加时间
    public void set(String key,Serializable value, Long time){
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        ops.set(key, value,time, TimeUnit.SECONDS);
    }
    //封装字符串获取的方法
    public Serializable get(String key){
        return redisTemplate.opsForValue().get(key);
    }
    //封装所有都能用的delete方法
    public void delete(String key){
        redisTemplate.delete(key);
    }
}
